package cn.tedu.medicalpath.scheduling.service.impl;

import cn.tedu.medicalpath.base.exception.ServiceException;
import cn.tedu.medicalpath.base.response.StatusCode;
import cn.tedu.medicalpath.employee.mapper.EmployeeMapper;
import cn.tedu.medicalpath.employee.pojo.entity.Employee;
import cn.tedu.medicalpath.scheduling.mapper.DoctorSchedulingMapper;
import cn.tedu.medicalpath.scheduling.pojo.dto.DoctorsSchedulingQuery;
import cn.tedu.medicalpath.scheduling.pojo.dto.DoctorsSchedulingSaveParam;
import cn.tedu.medicalpath.scheduling.pojo.entity.DoctorScheduling;
import cn.tedu.medicalpath.scheduling.pojo.vo.DoctorSchedulingVO;
import cn.tedu.medicalpath.scheduling.service.DoctorSchedulingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class DoctorSchedulingServiceImpl implements DoctorSchedulingService {

    @Autowired
    private DoctorSchedulingMapper doctorSchedulingMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<DoctorSchedulingVO> select(DoctorsSchedulingQuery doctorsSchedulingQuery) {
        log.debug("开始处理【查询医生排班】的业务，参数：{}",doctorsSchedulingQuery);
        List<DoctorSchedulingVO> list = doctorSchedulingMapper.select(doctorsSchedulingQuery);
        if(list.isEmpty()){
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }

    @Override
    public void insert(DoctorsSchedulingSaveParam doctorsSchedulingSaveParam) {
        log.debug("开始处理【新增医生排班】的业务，参数：{}",doctorsSchedulingSaveParam);
        DoctorScheduling doctorScheduling = new DoctorScheduling();
        BeanUtils.copyProperties(doctorsSchedulingSaveParam,doctorScheduling);
        Employee employee = employeeMapper.selectByName(doctorsSchedulingSaveParam.getName());
        doctorScheduling.setEmployeeId(employee.getId());
        if(doctorScheduling.getId() == null){
            doctorScheduling.setCreateTime(new Date());
            doctorSchedulingMapper.insert(doctorScheduling);
        }else {
            doctorScheduling.setUpdateTime(new Date());
            doctorSchedulingMapper.update(doctorScheduling);
        }
    }
}
